package com.lqs.blog.controller;

import com.lqs.blog.common.aop.LogAnnotation;
import com.lqs.blog.common.cache.Cache;
import com.lqs.blog.service.ArticleService;
import com.lqs.blog.service.ThreadService;
import com.lqs.blog.vo.param.ArticleParam;
import com.lqs.blog.vo.param.PageParams;
import com.lqs.blog.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

//json数据进行交互
@RestController
@RequestMapping("/articles")
public class ArticleController {
    @Autowired
    private ArticleService articleService;


    /**
     * 分页查询文章列表
     * 使用@RequestBody接收数据时，一般都用POST方式进行提交，接收前端传递的json数据
     *
     * @param pageParams
     * @return
     */
    @PostMapping
    //加上此注解 代表要对此接口记录日志
    @LogAnnotation(model = "文章", operator = "获取文章列表")//记录日志
    @Cache(expire = 5 * 60 * 1000, name = "listArticle")
    public Result listArticle(@RequestBody PageParams pageParams) {
//        int a = 1 / 0;
        System.out.println("--------------------");
        System.out.println(pageParams);
        return articleService.listArticle(pageParams);
    }

    @Cache(expire = 5 * 60 * 1000, name = "hot_article")//过期时间五分钟
    @PostMapping("hot")
    public Result hot() {
        //查询最热文章
        int limit = 5;
        return articleService.getHot(limit);
    }

    @Cache(expire = 5 * 60 * 1000, name = "news_article")
    @PostMapping("new")
    public Result newest() {
        int limit = 5;
        return articleService.getNewest(limit);
    }

    @PostMapping("listArchives")
    public Result listArchives() {
        //文章归档
        return articleService.listArchives();
    }

    @PostMapping("view/{id}")
    public Result view(@PathVariable("id") Long id) {
        //获取文章详情
        return articleService.getArticleById(id);
    }

    @PostMapping("publish")
    public Result publish(@RequestBody ArticleParam articleParam) {
        //发布文章
        return articleService.publishArticle(articleParam);
    }

}
